package com.cheetah.start.system.menu.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheetah.start.common.annotation.ApiCount;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.system.menu.domain.SysMenu;
import com.cheetah.start.system.menu.domain.SysMenuTree;
import com.cheetah.start.system.menu.service.SysMenuService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Feel
 * @date 2021/10/21 10:10
 * @Description:
 */
@Api(tags = "菜单管理crud")
@Slf4j
@ApiCount
@Validated
@CrossOrigin
@RestController
@RequestMapping("/menu")
public class SysBaseMenuController extends ResultController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 添加
     *
     * @param sysMenu 系统菜单
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:menu:add", orRole = "admin")
    @Log(title = "权限", describe = "添加", type = BusinessTypeEnum.ADD, logType = LogTypeEnum.OPERATE)
    @PostMapping("/add")
    public Result add(SysMenu sysMenu) {
        sysMenu.setEnable("1");
        return decide(sysMenuService.save(sysMenu), BusinessTypeEnum.ADD);
    }

    /**
     * 删除
     *
     * @param menuId 菜单id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:menu:delete", orRole = "admin")
    @Log(title = "权限", describe = "删除", type = BusinessTypeEnum.REMOVE, logType = LogTypeEnum.OPERATE)
    @DeleteMapping("/deleteOne")
    public Result delete(@NotNull(message = "ID不能为空") Integer menuId) {
        return decide(sysMenuService.removeById(menuId), BusinessTypeEnum.REMOVE);
    }


    /**
     * 更新
     *
     * @param sysMenu 系统菜单
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:menu:edit", orRole = "admin")
    @Log(title = "权限", describe = "更新", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    @PostMapping("/update")
    public Result update(@RequestBody SysMenu sysMenu) {
        if (sysMenu.getParentId() == null) {
            return failure("请选择上级菜单");
        }
        return decide(sysMenuService.updateById(sysMenu), BusinessTypeEnum.EDIT);
    }


    /**
     * 列表
     *
     * @param cond parent_id
     * @return {@link ResultTable}
     */
    @SaCheckPermission(value = "sys:menu:data", orRole = "admin")
    @GetMapping("/list")
    public ResultTable list(String cond) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(cond)) {
            wrapper.eq("parent_id", cond);
        }
        wrapper.orderByAsc("sort");
        List<SysMenu> list = sysMenuService.list(wrapper);
        return treeTable(list);
    }

    /**
     * 树列表
     *
     * @return {@link ResultTable}
     */
    @GetMapping("/treeList")
    public Result treeList() {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        List<SysMenu> list = sysMenuService.list(wrapper);
        List<SysMenuTree> sysMenuTrees = sysMenuService.copyTo(list);
        List<SysMenuTree> menuTrees = sysMenuService.menuTree(sysMenuTrees, 0);
        return success(menuTrees);
    }

}
